<!DOCTYPE HTML>
<html lang="zh-cmn-Hans">

<head>
	<title>大庆实验中学2018届毕业典礼现场互动</title>
    <meta charset='UTF-8'>
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" href="http://res.wx.qq.com/open/libs/weui/1.1.2/weui.min.css" />
    <style>
        body,
        html {
            height: 100%;
            background-color: #f8f8f8
        }
        
        body {
            font-family: -apple-system-font, Helvetica Neue, Helvetica, sans-serif
        }
        
        .item {
            padding: 10px 0
        }
        
        .item__title {
            margin-bottom: 5px;
            padding-left: 15px;
            padding-right: 15px;
            color: #999;
            font-weight: 400;
            font-size: 14px
        }
        
        .item__ctn {
            padding: 0 15px
        }
        
        .page_feedback {
            padding: 15px;
            overflow: auto;
            background-color: #FFF
        }
        
        label>* {
            pointer-events: none
        }
        
        .weui-picker__item {
            padding: 0;
            height: 34px;
            line-height: 34px
        }
    </style>
    <script type="text/javascript" src="http://res.wx.qq.com/open/libs/weuijs/1.1.3/weui.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        //判断字符串是否为空或均为空格
        function isNull(str) {
            if (str == "") return true;
            var regu = "^[ ]+$";
            var re = new RegExp(regu);
            return re.test(str);
        }
        //服务器IP与端口
        window.server_ip = "shinrariel.com";
        window.server_port = "7172";

        //主函数，点击按钮后调用
        function confirm() {
            var user_input = document.getElementById("danmaku").value;
            var bj_time = "";
            //验证表单是否为空
            weui.form.validate('#danmaku', function() {
                window.loading = weui.loading('发送中...');
                if (isNull(window.user_input)) {
                    //为空后的操作
                    setTimeout(function() {
                        loading.hide();
                        //显示“弹幕内容不能为空”弹框
                        var empty_string_err = weui.dialog({
                            title: '错误',
                            content: '弹幕内容不能为空',
                            className: 'empty-string-error-dialog',
                            buttons: [{
                                label: '确定',
                                type: 'primary',
                                onClick: function() {
                                    empty_string_err.hide();
                                }
                            }]
                        });
                        //变量恢复初始值
                        user_input = "";
                    }, 500);
                }
                return true;
            });
            //尝试获取时间
            var timexhr = $.get("http://quan.suning.com/getSysTime.do", function() {}, "text");
            timexhr.done(function(data) {
                var timeobj = JSON.parse(data);
                //获取时间码，并转换为字符串
                bj_time = timeobj.sysTime2.toString();
                //输出调试信息
                console.log("time=" + bj_time + ",input=" + user_input);
                //尝试发送消息
                var postxhr = $.post("http://" + window.server_ip + ":" + window.server_port + "/1/push/all", JSON.stringify({
                   // "name": "danmakuuser",
					"time": bj_time,
                    "msg_body": user_input
                }), function() {}, "json");
                //发送成功显示
                postxhr.done(function(data) {
                    setTimeout(function() {
                        window.loading.hide();
                        weui.toast('操作成功', 1000);
                    }, 1000);
                    //变量恢复初始值
                    bj_time = "";
                    user_input = "";
                });
                postxhr.fail(function() {
                    //显示发送失败错误
                    setTimeout(function() {
                        window.loading.hide()
                        var send_msg_err = weui.dialog({
                            title: '错误',
                            content: '与服务器连接失败，请检查网络并重试',
                            className: 'send-msg-error-dialog',
                            buttons: [{
                                label: '确定',
                                type: 'primary',
                                onClick: function() {
                                    send_msg_err.hide();
                                }
                            }]
                        });
                    }, 500);
                    //变量恢复初始值
                    bj_time = "";
                    user_input = "";
                    return;
                });
                return;
            });
            timexhr.fail(function() {
                //显示“获取时间错误”弹框
                setTimeout(function() {
                    window.loading.hide()
                    var get_time_err = weui.dialog({
                        title: '错误',
                        content: '获取时间错误，请检查网络并重试',
                        className: 'get-time-error-dialog',
                        buttons: [{
                            label: '确定',
                            type: 'primary',
                            onClick: function() {
                                get_time_err.hide();
                            }
                        }]
                    });
                }, 500);
                //变量恢复初始值
                bj_time = "";
                user_input = "";
                //直接返回，不继续执行
                return;
            });
        }
    </script>
</head>

<body>
    <div class="page">
        <h1 class="page__title" style="text-align:center">大庆实验中学2018届毕业典礼现场互动站</h1>
        <h2 class="page__title" style="color:#888;text-align:center">Powered By 神罗瑞尔科技</h2>
    </div>
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__hd">
                <label for="" class="weui-label">弹幕内容</label>
            </div>
            <div class="weui-cell__bd">
                <input id="danmaku" class="weui-input" type="text" placeholder="请在这里输入你想要发送的弹幕">
            </div>
        </div>
    </div>
    <div class="weui-btn-area">
        <a id="formSubmitBtn" href="javascript:confirm()" class="weui-btn weui-btn_primary">发送</a>
    </div>

</body>

</html>